//
//  DMTool.m
//  DramaProject
//
//  Created by admin on 16/7/19.
//  Copyright © 2016年 xll. All rights reserved.
//

#import "DMTool.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
/**
 An array of NSNumber objects, shows the best order for path scale search.
 e.g. iPhone3GS:@[@1,@2,@3] iPhone5:@[@2,@3,@1]  iPhone6 Plus:@[@3,@2,@1]
 */
static NSArray *_NSBundlePreferredScales() {
    static NSArray *scales;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        CGFloat screenScale = [UIScreen mainScreen].scale;
        if (screenScale <= 1) {
            scales = @[@1,@2,@3];
        } else if (screenScale <= 2) {
            scales = @[@2,@3,@1];
        } else {
            scales = @[@3,@2,@1];
        }
    });
    return scales;
}
/**
 Add scale modifier to the file name (without path extension),
 From @"name" to @"name@2x".
 
 e.g.
 <table>
 <tr><th>Before     </th><th>After(scale:2)</th></tr>
 <tr><td>"icon"     </td><td>"icon@2x"     </td></tr>
 <tr><td>"icon "    </td><td>"icon @2x"    </td></tr>
 <tr><td>"icon.top" </td><td>"icon.top@2x" </td></tr>
 <tr><td>"/p/name"  </td><td>"/p/name@2x"  </td></tr>
 <tr><td>"/path/"   </td><td>"/path/"      </td></tr>
 </table>
 
 @param scale Resource scale.
 @return String by add scale modifier, or just return if it's not end with file name.
 */
static NSString *_NSStringByAppendingNameScale(NSString *string, CGFloat scale) {
    if (!string) return nil;
    if (fabs(scale - 1) <= __FLT_EPSILON__ || string.length == 0 || [string hasSuffix:@"/"]) return string.copy;
    return [string stringByAppendingFormat:@"@%@x", @(scale)];
}
@implementation DMTool

+(UIImageView *)CreateImgViewWithFrame:(CGRect)frame withImgName:(NSString *)imgName withContentMode:(UIViewContentMode)mode
{
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:frame];
    if(imgName){
        if (![imgName isEqualToString:@""]) {
            NSString *path = [self GetImagePathWithImgStr:imgName];
            if (path) {
                UIImage *image = [UIImage imageWithContentsOfFile:path];
                imageView.image = image;
            }else {
                imageView.image = [UIImage imageNamed:imgName];
            }
        }
    }
    imageView.contentMode = mode;
    imageView.userInteractionEnabled = YES;
    return imageView;
}
/**
 *  根据image字符串 查看是否有file路径 如果有 直接返回路径 如果没有 直接返回nil
 *
 *  @param name image文件字符串
 *
 *  @return 路径
 */
+(NSString *)GetImagePathWithImgStr:(NSString *)name
{
    NSString *res = name.stringByDeletingPathExtension;
    NSString *ext = name.pathExtension;
    NSString *path = nil;
    CGFloat scale = 1;
    
    // If no extension, guess by system supported (same as UIImage).
    NSArray *exts = ext.length > 0 ? @[ext] : @[@"", @"png", @"jpeg", @"jpg", @"gif", @"webp", @"apng"];
    NSArray *scales = _NSBundlePreferredScales();
    for (int s = 0; s < scales.count; s++) {
        scale = ((NSNumber *)scales[s]).floatValue;
        NSString *scaledName = _NSStringByAppendingNameScale(res, scale);
        for (NSString *e in exts) {
            path = [[NSBundle mainBundle] pathForResource:scaledName ofType:e];
            if (path) break;
        }
        if (path) break;
    }
    if (path.length == 0) return nil;
    return path;
}
+(UILabel *)CreateLabelWithFrame:(CGRect)frame withText:(NSString *)text withFont:(UIFont *)font withTextAlign:(NSTextAlignment)textAlignment
{
    
    if (text&&![text isKindOfClass:[NSString class]]) {
        text = [NSString stringWithFormat:@"%@",text];
    }
    
    UILabel * label = [[UILabel alloc] initWithFrame:frame];
    if (text&&![text isKindOfClass:[NSNull class]]) {
        
        label.text = text;
    }
    label.font = font;
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByTruncatingTail;
    label.textAlignment = textAlignment;
    return label;
}
+(UIButton *)CreateBtnWithFrame:(CGRect)frame withBGImg:(NSString *)bgImage withImg:(NSString *)img withTitle:(NSString *)title selector:(SEL)method target:(id)target
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.imageView.contentMode = UIViewContentModeScaleAspectFill;
    button.frame = frame;
    if(bgImage){
        NSString *path = [self GetImagePathWithImgStr:bgImage];
        if (path) {
            [button setBackgroundImage:[UIImage imageWithContentsOfFile:path] forState:UIControlStateNormal];
        }else {
            [button setBackgroundImage:[UIImage imageNamed:bgImage] forState:UIControlStateNormal];
        }
    }
    if(img){
        NSString *path = [self GetImagePathWithImgStr:img];
        if (path) {
            [button setImage:[UIImage imageWithContentsOfFile:path] forState:UIControlStateNormal];
        }else {
            [button setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
        }
    }
    if(title && ![title isKindOfClass:[NSNull class]]){
        [button setTitle:title forState:UIControlStateNormal];
    }
    if (target && method) {
        [button addTarget:target action:method forControlEvents:UIControlEventTouchUpInside];
    }
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    return button;
}
+(UIButton *)CreateBtnWithFrame:(CGRect)frame withNormalImg:(NSString *)normalImg withSelectedImg:(NSString *)seletedImg selector:(SEL)method target:(id)target
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.imageView.contentMode = UIViewContentModeScaleAspectFill;
    button.frame = frame;
    if(normalImg){
        NSString *path = [self GetImagePathWithImgStr:normalImg];
        if (path) {
            [button setImage:[UIImage imageWithContentsOfFile:path] forState:UIControlStateNormal];
        }else {
            [button setImage:[UIImage imageNamed:normalImg] forState:UIControlStateNormal];
        }
        
    }
    if(seletedImg){
        NSString *path = [self GetImagePathWithImgStr:seletedImg];
        if (path) {
            [button setImage:[UIImage imageWithContentsOfFile:path] forState:UIControlStateSelected];
        }else {
            [button setImage:[UIImage imageNamed:seletedImg] forState:UIControlStateSelected];
        }
    }
    if (target && method) {
        [button addTarget:target action:method forControlEvents:UIControlEventTouchUpInside];
    }
    return button;
}
+(UIButton *)CreateBtnWithFrame:(CGRect)frame withNormalText:(NSString *)normalText withSelectedText:(NSString *)seletedText selector:(SEL)method target:(id)target
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = frame;
    if(normalText && ![normalText isKindOfClass:[NSNull class]]){
        [button setTitle:normalText forState:UIControlStateNormal];
    }
    if(seletedText && ![seletedText isKindOfClass:[NSNull class]]){
        [button setTitle:seletedText forState:UIControlStateSelected];
    }
    if (target && method) {
        [button addTarget:target action:method forControlEvents:UIControlEventTouchUpInside];
    }
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    return button;
}
+(UITableView *)CreateTableViewWithFrame:(CGRect)frame withDataSourceAndDelegate:(id)delegate withStyle:(UITableViewStyle)style
{
    UITableView *tableView = [[UITableView alloc]initWithFrame:frame style:style];
    tableView.delegate = delegate;
    tableView.dataSource = delegate;
    return tableView;
}
+(UITextField *)CreateTextFieldWithFrame:(CGRect)frame withText:(NSString *)text withPlacehold:(NSString *)placehold withFont:(UIFont *)font withTextAlign:(NSTextAlignment)textAlignment
{
    UITextField *textField = [[UITextField alloc]initWithFrame:frame];
    textField.text = text;
    textField.placeholder = placehold;
    textField.font = font;
    textField.textAlignment = textAlignment;
    return textField;
}
+(UIImage*) createImageWithColor:(UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}
+ (void)readQRCodeFromImage:(UIImage *)qrCodeImage myQRCode:(void(^)(NSString *qrString,NSError *error))myQRCode
{
    
    UIImage * srcImage = qrCodeImage;
    if (nil == srcImage) {
        myQRCode(nil,[NSError errorWithDomain:@"未传入图片" code:0 userInfo:nil]);
        return;
    }
    
    CIContext *context = [CIContext contextWithOptions:nil];
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:context options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}];
    CIImage *image = [CIImage imageWithCGImage:srcImage.CGImage];
    NSArray *features = [detector featuresInImage:image];
    if (features.count) {
        CIQRCodeFeature *feature = [features firstObject];
        
        NSString *result = feature.messageString;
        
        myQRCode(result,nil);
    }
    else{
        myQRCode(nil,[NSError errorWithDomain:@"未能识别出二维码" code:0 userInfo:nil]);
        return;
    }
    
}
+ (NSString *)getAstroWithMonth:(int)m day:(int)d
{
    NSString *astroString = @"魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯";
    NSString *astroFormat = @"102123444543";
    NSString *result;
    if (m<1||m>12||d<1||d>31){
        return @"错误日期格式!";
    }
    if(m==2 && d>29)
    {
        return @"错误日期格式!!";
    }else if(m==4 || m==6 || m==9 || m==11) {
        if (d>30) {
            return @"错误日期格式!!!";
        }
    }
    result=[NSString stringWithFormat:@"%@",[astroString substringWithRange:NSMakeRange(m*2-(d < [[astroFormat substringWithRange:NSMakeRange((m-1), 1)] intValue] - (-19))*2,2)]];
    
    if ([result isEqualToString:@"魔羯"]) {
        return @"魔羯";
    }
    else if ([result isEqualToString:@"水瓶"])
    {
        return @"水瓶";
    }
    else if ([result isEqualToString:@"双鱼"])
    {
        return @"双鱼";
    }
    else if ([result isEqualToString:@"白羊"])
    {
        return @"白羊";
    }
    else if ([result isEqualToString:@"金牛"])
    {
        return @"金牛";
    }
    else if ([result isEqualToString:@"双子"])
    {
        return @"双子";
    }
    else if ([result isEqualToString:@"巨蟹"])
    {
        return @"巨蟹";
    }
    else if ([result isEqualToString:@"狮子"])
    {
        return @"狮子";
    }
    else if ([result isEqualToString:@"处女"])
    {
        return @"处女";
    }
    else if ([result isEqualToString:@"天秤"])
    {
        return @"天秤";
    }
    else if ([result isEqualToString:@"天蝎"])
    {
        return @"天蝎";
    }
    else if ([result isEqualToString:@"射手"])
    {
        return @"射手";
    }
    else
    {
        return @"";
    }
    
    
    return result;
}
+(NSArray *)handleImages:(NSArray *)images isCompress:(BOOL)compress fileDirectory:(NSString *)directory
{
    if (!directory) {
        NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *imageDirectory = [NSString stringWithFormat:@"%@/imageCache",[array lastObject]];
        if (![[NSFileManager defaultManager] fileExistsAtPath:imageDirectory]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:imageDirectory withIntermediateDirectories:YES attributes:nil error:nil];
        }
        directory = imageDirectory;
    }
    else
    {
        if (![[NSFileManager defaultManager] fileExistsAtPath:directory]) {
            [[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];
        }
    }
    
    NSMutableArray *filePathArray = [NSMutableArray arrayWithCapacity:0];
    for (int i = 0; i < images.count; i++) {
        UIImage *image = [images objectAtIndex:i];
        NSString *fileName = [NSString stringWithFormat:@"photo_%lld.jpg",(long long)([[NSDate date] timeIntervalSince1970] * 1000)];
        //图片存储路径
        NSString *  filePath = [NSString stringWithFormat:@"%@/%@",directory,fileName];
        NSString *p = [self handleImage:image isCompress:compress filePath:filePath];
        if (p) {
            [filePathArray addObject:p];
        }
    }
    return filePathArray;
    
}
+(NSString *)handleImage:(UIImage *)image isCompress:(BOOL)compress filePath:(NSString *)filePath
{
    if (!image) {
        return nil;
    }
    //图片旋转
    if (image.imageOrientation != UIImageOrientationUp) {
        //图片旋转
        image = [self fixOrientation:image];
    }
    NSData *imageData;
    if (compress) {//原图不压缩
        //图片压缩
        imageData = [self compressImage:image];
    }
    else
    {
        imageData = UIImageJPEGRepresentation(image, 1.0);
    }
    
   BOOL success =  [[NSFileManager defaultManager]createFileAtPath:filePath contents:imageData attributes:nil];
//    BOOL success = [imageData writeToFile:filePath atomically:YES];

    image = nil;
    imageData = nil;
    if (success)
    {
        return filePath;
    }
    else
    {
        NSLog(@"图片保存失败!");
        return nil;
    }
}
//图片压缩
+ (NSData *)compressImage:(UIImage *)image
{
    
    NSData * data = UIImageJPEGRepresentation(image, 1);
    long long size = data.length/1024;
    //    if (size>4*1024) {//大于4M
    //        data = UIImageJPEGRepresentation(image, 0.1);
    //
    //    }else if (size>3*1024) {//大于3M
    //        data = UIImageJPEGRepresentation(image, 0.2);
    //
    //    }else if (size>2*1024) {//大于2M
    //        data = UIImageJPEGRepresentation(image, 0.3);
    //
    //    }else if (size>1024) {//大于1M
    //        data = UIImageJPEGRepresentation(image, 0.4);
    //
    //    }else if (size>0.5*1024) {//大于0.5M
    //        data = UIImageJPEGRepresentation(image, 0.6);
    //
    //    }else if (size>0.2*1024) {//大于0.2M
    //        data = UIImageJPEGRepresentation(image, 0.8);
    //
    //    }else {
    //        data = UIImageJPEGRepresentation(image, 1.0);
    //
    //    }
    if (size>1024 * 1.0) {//大于1M
        data = UIImageJPEGRepresentation(image, 1/5);
        
    }else if (size>0.8*1024) {//大于0.5M
        data = UIImageJPEGRepresentation(image, 1/4);
        
    }else if (size>0.4*1024) {//大于0.2M
        data = UIImageJPEGRepresentation(image, 1/3);
        
    }else {
        data = UIImageJPEGRepresentation(image, 1.0);
        
    }
    return data;
}
//图片旋转处理
+ (UIImage *)fixOrientation:(UIImage *)aImage
{
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
            
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
            
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        default:
            break;
    }
    
    switch (aImage.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
            
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        default:
            break;
    }
    
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
                                             CGImageGetBitsPerComponent(aImage.CGImage), 0,
                                             CGImageGetColorSpace(aImage.CGImage),
                                             CGImageGetBitmapInfo(aImage.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (aImage.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
            break;
            
        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
            break;
    }
    
    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}
//Obj-c
+ (void)combVideos:(NSArray *)videoFileArray outputPath:(NSString *)outputPath withPreset:(NSString *)preset completion:(void(^)(BOOL,NSString *))completion
{
    if (videoFileArray.count == 0) {
        completion(NO,outputPath);
        NSLog(@"没有找到视频");
        return;
    }
    if (videoFileArray.count == 1) {
        if (completion) {
            completion(YES,[videoFileArray firstObject]);
        }
        return;
    }
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        CGSize renderSize = CGSizeMake(352.0, 288.0);
        
        
        for (NSString *filePath in videoFileArray)
        {
            NSURL *fileURL = [NSURL fileURLWithPath:filePath];
            AVAsset *asset = [AVAsset assetWithURL:fileURL];
            NSArray *arr = [asset tracksWithMediaType:@"vide"];
            
            if (arr.count > 0) {
                AVAssetTrack *assetTrack  = [arr objectAtIndex:0];
                renderSize.width = MAX(renderSize.width, assetTrack.naturalSize.height);
                renderSize.height = MAX(renderSize.height, assetTrack.naturalSize.width);
            }
        }
        
        AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
        
        AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
        
        AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                            preferredTrackID:kCMPersistentTrackID_Invalid];
        AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                            preferredTrackID:kCMPersistentTrackID_Invalid];
        
        CMTime totalDuration = kCMTimeZero;
        
        for (int i = 0; i < videoFileArray.count; i++) {
            AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:videoFileArray[i]]];
            NSError *erroraudio = nil;//获取AVAsset中的音频 或者视频
            AVAssetTrack *assetAudioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] firstObject];//向通道内加入音频或者视频
            //                BOOL ba =
            [audioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                                ofTrack:assetAudioTrack
                                 atTime:totalDuration
                                  error:&erroraudio];
            
            //        NSLog(@"erroraudio:%@%d",erroraudio,ba);
            NSError *errorVideo = nil;
            AVAssetTrack *assetVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo]firstObject];
            
            //                BOOL bl =
            [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration)
                                ofTrack:assetVideoTrack
                                 atTime:totalDuration
                                  error:&errorVideo];
            
            //        NSLog(@"errorVideo:%@%d",errorVideo,bl);
            totalDuration = CMTimeAdd(totalDuration, asset.duration);
            
            videoComposition.frameDuration = CMTimeMake(1, 30);
            //视频输出尺寸
            videoComposition.renderSize =renderSize;
            videoComposition.renderScale = 1.0;
            
            AVMutableVideoCompositionInstruction * avMutableVideoCompositionInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
            
            [avMutableVideoCompositionInstruction setTimeRange:CMTimeRangeMake(kCMTimeZero, [mixComposition duration])];
            
            AVMutableVideoCompositionLayerInstruction * avMutableVideoCompositionLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:assetAudioTrack];
            
            [avMutableVideoCompositionLayerInstruction setTransform:assetVideoTrack.preferredTransform atTime:kCMTimeZero];
            
            avMutableVideoCompositionInstruction.layerInstructions = [NSArray arrayWithObject:avMutableVideoCompositionLayerInstruction];
            
            videoComposition.instructions = [NSArray arrayWithObject:avMutableVideoCompositionInstruction];
            
            
        }

        NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:mixComposition];
        NSString *p = preset;
        if (![compatiblePresets containsObject:preset]) {
            if (compatiblePresets.count > 0) {
                p = [compatiblePresets firstObject];
            }
            if (completion) {
                completion(NO,outputPath);
            }
        }
        if ([[NSFileManager defaultManager]fileExistsAtPath:outputPath]) {
            [[NSFileManager defaultManager]removeItemAtPath:outputPath error:nil];
        }
        
        NSURL *mergeFileURL = [NSURL fileURLWithPath:outputPath];
        AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:p];
        exporter.outputURL = mergeFileURL;
        
        exporter.videoComposition = videoComposition;
        exporter.outputFileType = AVFileTypeMPEG4;
        exporter.shouldOptimizeForNetworkUse = YES;
        [exporter exportAsynchronouslyWithCompletionHandler:^{
            NSLog(@" exporter%@",exporter.error);
            dispatch_async(dispatch_get_main_queue(), ^{
                if (exporter.status == AVAssetExportSessionStatusCompleted) {
                    if (completion) {
                        completion(YES,outputPath);
                    }
                }
                else if(exporter.status == AVAssetExportSessionStatusFailed || exporter.status == AVAssetExportSessionStatusCancelled)
                {
                    if (completion) {
                        completion(NO,outputPath);
                    }
                }
            });
            
        }];
    });
}
+(UIImage *)GetLocalVideoThumbImgFromPath:(NSString *)filePath
{
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
                                             initWithContentURL:[NSURL fileURLWithPath:filePath]];
    UIImage *img = [moviePlayer thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
    return img;
}
+(UIImage *)GetNetVideoThumbImgFromUrl:(NSString *)url
{
    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:url]];
    moviePlayer.shouldAutoplay = NO;
    UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:0.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
    return thumbnail;
}
+(NSString *)fileSizeFormatter:(long long)size
{
    if (size == 0) {
        return  @"";
    }
    else if (size < 1024.0) {
        return [NSString stringWithFormat:@"%.2fb",size * 1.0];
    }
    else if(size < 1024.0 * 1024)
    {
        return [NSString stringWithFormat:@"%.2fk",size / 1024.0];
    }
    else if(size < 1024.0 * 1024 * 1024)
    {
        return [NSString stringWithFormat:@"%.2fM",size / 1024.0 / 1024.0];
    }
    else
    {
        return [NSString stringWithFormat:@"%.2fG",size / 1024.0 / 1024.0 / 1024.0];
    }
    return @"";
}
@end



